package com.groupbuying.settleconsumer.service.impl.settleType;

import com.groupbuying.settleconsumer.common.ChannelServiceStrategyFactory;
import com.groupbuying.settleconsumer.common.SettleServiceStrategyFactory;
import com.groupbuying.settleconsumer.connector.SysconfigFeign;
import com.groupbuying.settleconsumer.constants.PayInfoEnumConstants;
import com.groupbuying.settleconsumer.domain.SettleUshopSplitRluesDO;
import com.groupbuying.settleconsumer.service.ChannelService;
import com.groupbuying.settleconsumer.service.SettleService;
import com.groupbuying.settleconsumer.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UshopGyydSettleService implements SettleService, InitializingBean {

    public static final Logger logger = LoggerFactory.getLogger(UshopGyydSettleService.class);

    @Autowired
    SysconfigFeign sysconfigFeign;

    @Override
    public SettleUshopSplitRluesDO getRule(Map params){
        String orderId = (String) params.get("orderId");//订单号
        String ruleId = (String) params.get("ruleId");
        String payType = (String) params.get("payType");//支付渠道
        String storeId = (String) params.get("storeId");
        String settleRuleId = (String) params.get("settleRuleId");
        String payChannel = (String) params.get("payChannel");
        //查询优惠规则表
        BigDecimal backCurrencyRatio ;//返积分比例
        BigDecimal platformRatio ;//平台抽成比例
        //根据支付渠道和支付类型，设置通道费和平台补贴费
        BigDecimal channleRatio ;//通道费费率
        BigDecimal subsidiesRatio ;//平台补贴费费率
        ChannelService channelService = ChannelServiceStrategyFactory.getByChannelType(payChannel);
        Map<String,BigDecimal> channelRatios = channelService.getChannelRate();
        channleRatio = channelRatios.get("channleRatio");
        subsidiesRatio = channelRatios.get("subsidiesRatio");
        //商户结算比例
        BigDecimal customerRatio;
        try{
            //通过查询sysConfig中心的优惠规则表和商户抽成规则表生成分账规则表
            Map<String,Object> ruleParam = new HashMap<>();
            ruleParam.put("ruleId",ruleId);
            // 抽成规则表
            logger.error("UshopGyydSettleService.initializeRuleParams-->getOpexRule "+orderId+" params:::"+ JsonUtils.objectToJson(ruleParam));
            Map<String,Object> opexRule = sysconfigFeign.getOpexRule(ruleParam);
            logger.error("UshopGyydSettleService.initializeRuleParams-->getOpexRule "+orderId+" result:::"+JsonUtils.objectToJson(opexRule));
            double currencyExtracting = opexRule.get("currencyExtracting") == null ? 0.00 : (double) opexRule.get("currencyExtracting");
            double onlineExtracting = opexRule.get("onlineExtracting") == null ? 0.00 : (double) opexRule.get("onlineExtracting");
            double storeSettlement =  opexRule.get("storeSettlement") == null ? 0.00 : (double) opexRule.get("storeSettlement");
            double opexRuleBackCurrencyRatio =  opexRule.get("backCurrencyRatio") == null ? 0.00 : (double) opexRule.get("backCurrencyRatio");
            if(PayInfoEnumConstants.ENUM_PAYTYPE_CUR.equals(payType)){//积分支付
                logger.error("UshopGyydSettleService.initializeRuleParams 积分支付");
                platformRatio = new BigDecimal(currencyExtracting).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_UP);
                backCurrencyRatio = new BigDecimal("0.00");
            }else{//线上支付
                logger.error("UshopGyydSettleService.initializeRuleParams 线上支付");
                platformRatio = new BigDecimal(onlineExtracting).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_UP);
                backCurrencyRatio = new BigDecimal(opexRuleBackCurrencyRatio).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_UP);
            }
            customerRatio = new BigDecimal(storeSettlement).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_UP);

            //分账规则表中ruleId与优惠规则表和抽成规则表没有关系，会自己生成ruleId,并与分账主表做关联
            SettleUshopSplitRluesDO settleUshopSplitRluesDO = new SettleUshopSplitRluesDO();
            settleUshopSplitRluesDO.setCustomerId(storeId);
            settleUshopSplitRluesDO.setRluesId(settleRuleId);
            settleUshopSplitRluesDO.setVirtualRatio(backCurrencyRatio);
            settleUshopSplitRluesDO.setPlatformRatio(platformRatio);

            settleUshopSplitRluesDO.setChannleRatio(channleRatio);
            settleUshopSplitRluesDO.setSubsidiesRatio(subsidiesRatio);

            settleUshopSplitRluesDO.setCustomerRatio(customerRatio);
            settleUshopSplitRluesDO.setIsDelete("1");
            settleUshopSplitRluesDO.setGmtCreate(new Date());
            logger.error("UshopGyydSettleService-->initializeRuleParams return SettleSplitRluesDO:"+settleUshopSplitRluesDO);
            return settleUshopSplitRluesDO;
        }catch (Exception e){
            logger.error("UshopGyydSettleService-->initializeRuleParams "+orderId+" sysconfigFeign fail",e);
            return null;
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        SettleServiceStrategyFactory.register("UshopGyydSettle",this);
    }
}
